package com.lagou.zk.zkconfigure.util;

import com.lagou.zk.zkconfigure.ZkconfigureApplication;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcUtil {

    /**
     * 获取链接
     * @return
     * @throws SQLException
     */
    public static Connection getConn() throws SQLException {
        String url = ZkconfigureApplication.dataSource.getUrl();
        String username = ZkconfigureApplication.dataSource.getUsername();
        String password = ZkconfigureApplication.dataSource.getPassword();
        System.out.println(String.format("数据库信息 url: %s |username: %s | password: %s",url,username,password));
        return ZkconfigureApplication.dataSource.getConnection();
    }

    /**
     * 关闭链接
     * @param conn
     * @param state
     * @param result
     */
    public static void close(Connection conn, Statement state, ResultSet result) {

        try {
            if (result != null) {
                result.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (state != null) {
                        state.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
